package com.zhuiyun.project.api.collectVideo.controller;

import cn.hutool.json.JSONObject;
import com.zhuiyun.project.annotation.OptLog;
import com.zhuiyun.project.api.collectVideo.model.CollectVideoModel;
import com.zhuiyun.project.api.collectVideo.service.CollectVideoService;
import com.zhuiyun.project.common.errorCode.CommonException;
import com.zhuiyun.project.common.response.CommonResult;
import com.zhuiyun.project.utils.constants.CommonConstants;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

/**
 * @ClassName CollectVideoController
 * @Description TODO 前端用户收藏模块
 * @Author LXJ
 * @Date 2023/12/28 8:57
 **/
@RestController
@RequestMapping("collectVideoController")
@Api(tags = "前端用户收藏模块")
public class CollectVideoController {
    @Autowired
    private CollectVideoService collectVideoService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;


    /**
    * @Author XC8
    * @Description 根据用户id查询倒序收藏列表
    * @Date 2023/12/29 10:38
    * @Return com.zhuiyun.project.common.response.CommonResult
    */
    @GetMapping("/getCollectVideoById")
    @ApiOperation(value = "根据id用户信息")
    public CommonResult getCollectVideoById(String token,String videoTopic,String videoDesc,Integer Favorites) throws CommonException{
        String jsonstr = stringRedisTemplate.opsForValue().get(token);
        if (jsonstr == null || jsonstr.equals("")) {
            return CommonResult.error(404,"用户信息失效,请重新登录");
        } else {
            // 使用JSONObject解析ISON字符串
            JSONObject json0bject = new JSONObject(jsonstr);
            // 获取id字段的值
            int userId = json0bject.getInt("id");
            return CommonResult.ok(collectVideoService.getCollectVideoById(userId,videoTopic,videoDesc,Favorites));
        }
    }

    /**
     * @Author XC8
     * @Description 根据用户id查询正序收藏列表
     * @Date 2023/12/29 10:38
     * @Return com.zhuiyun.project.common.response.CommonResult
     */
    @GetMapping("/getCollectVideoByIdASC")
    @ApiOperation(value = "根据id用户信息")
    public CommonResult getCollectVideoByIdASC(String token,String videoTopic,String videoDesc,Integer Favorites) throws CommonException{
        String jsonstr = stringRedisTemplate.opsForValue().get(token);
        if (jsonstr == null || jsonstr.equals("")) {
            return CommonResult.error(404,"用户信息失效,请重新登录");
        } else {
            // 使用JSONObject解析ISON字符串
            JSONObject json0bject = new JSONObject(jsonstr);
            // 获取id字段的值
            int userId = json0bject.getInt("id");
            return CommonResult.ok(collectVideoService.getCollectVideoByIdASC(userId,videoTopic,videoDesc,Favorites));
        }

    }

    /**
    * @Author XC8
    * @Description 收藏
    * @Date 2024/3/8 9:45
    * @Return com.zhuiyun.project.common.response.CommonResult
    */
    @PutMapping("addCollectVideo")
    @ApiOperation(value = "前端收藏")
    public CommonResult addCollectVideo(@RequestBody CollectVideoModel collectVideoModel)throws CommonException{
        return CommonResult.ok(collectVideoService.addCollectVideo(collectVideoModel));
    }

    /**
    * @Author XC8
    * @Description 取消收藏
    * @Date 2024/3/8 9:46
    * @Return com.zhuiyun.project.common.response.CommonResult
    */
    @PostMapping("collectVideoDelete")
    @OptLog(modular = "收藏",type = CommonConstants.DELETE,describe = "取消收藏")
    public CommonResult collectVideoDelete(@RequestBody CollectVideoModel collectVideoModel)throws CommonException{
        return collectVideoService.collectVideoDelete(collectVideoModel);
    }

}
